package com.kwq.registered.tools;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

@Component
@Repository
public class RedisTool {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 存实体类
     * @param key key
     * @param o 实体类对象
     */
    public void setEntity(String key, Object o) {
        redisTemplate.opsForValue().set(key, o);
    }

    /**
     * 存实体类
     * @param key key
     * @param o 实体类对象
     * @param time 数据存储时间 单位分钟
     */
    public void setEntity(String key, long time, Object o) {
        redisTemplate.opsForValue().set(key, o, time, TimeUnit.MINUTES);
    }

    /**
     * 获取 CODE
     * @param key
     * @return UserVo
     */
    public String getCode(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    /**
     * 获取实体类对象
     * @param key
     * @param entity
     * @param <T>
     * @return
     */
    public <T> T getEntity(String key, Class<T> entity) {
        return (T) redisTemplate.opsForValue().get(key);
    }
}
